﻿
Public Class Validations


    Public Function resetTextBox(ByVal form As Form) As Boolean
        For Each Control As Control In form.Controls
            If TypeOf Control Is GroupBox Then
                For Each ChildControl As Control In Control.Controls
                    If TypeOf ChildControl Is TextBox Then
                        If ChildControl.Enabled() = True Then
                            Dim tmpTB As TextBox
                            tmpTB = CType(ChildControl, TextBox)
                            tmpTB.Text = ""
                        End If
                    End If
                Next ChildControl
            End If
        Next Control
        Return True
    End Function

    Public Function resetComboBox(ByVal form As Form) As Boolean
        For Each Control As Control In form.Controls
            If TypeOf Control Is GroupBox Then
                For Each ChildControl As Control In Control.Controls
                    If TypeOf ChildControl Is ComboBox Then
                        If ChildControl.Enabled() = True Then
                            Dim tmpCmb As ComboBox
                            tmpCmb = CType(ChildControl, ComboBox)
                            tmpCmb.SelectedIndex = -1
                        End If
                    End If
                Next ChildControl
            End If
        Next Control
        Return True
    End Function

    Public Function resetCheckBox(ByVal form As Form) As Boolean
        For Each Control As Control In form.Controls
            If TypeOf Control Is GroupBox Then
                For Each ChildControl As Control In Control.Controls
                    If TypeOf ChildControl Is CheckBox Then
                        If ChildControl.Enabled() = True Then
                            Dim tmpChk As CheckBox
                            tmpChk = CType(ChildControl, CheckBox)
                            tmpChk.Checked = False
                        End If
                    End If
                Next ChildControl
            End If
        Next Control
        Return True
    End Function

    Public Function resetRdButton(ByVal form As Form) As Boolean
        For Each Control As Control In form.Controls
            If TypeOf Control Is GroupBox Then
                For Each ChildControl As Control In Control.Controls
                    If TypeOf ChildControl Is RadioButton Then
                        If ChildControl.Enabled() = True Then
                            Dim tmpRB As RadioButton
                            tmpRB = CType(ChildControl, RadioButton)
                            tmpRB.Checked = False
                        End If
                    End If
                Next ChildControl
            End If
        Next Control
        Return True
    End Function

    Public Function validarNoNull(ByVal form As Form) As Boolean
        For Each Control As Control In form.Controls
            If TypeOf Control Is GroupBox Then
                For Each ChildControl As Control In Control.Controls
                    If TypeOf ChildControl Is TextBox Then
                        If (ChildControl.Enabled() = True And ChildControl.Text = "") Then
                            MessageBox.Show("Algunos campos estan vacíos")
                            SetTheRegion(ChildControl, 2, 2, 4)
                            ChildControl.Focus()
                            Return False
                        End If
                    End If
                Next ChildControl
            End If
        Next Control
        Return True
    End Function

    Public Function validaComboBox(ByVal form As Form) As Boolean
        For Each Control As Control In form.Controls
            If TypeOf Control Is GroupBox Then
                For Each ChildControl As Control In Control.Controls
                    If TypeOf ChildControl Is ComboBox Then
                        If ChildControl.Enabled() = True Then
                            Dim tmpCmb As ComboBox
                            tmpCmb = CType(ChildControl, ComboBox)
                            If (tmpCmb.SelectedIndex = -1) Then
                                MessageBox.Show("Falta seleccionar algun dato")
                                SetTheRegion(ChildControl, 2, 2, 4)
                                ChildControl.Focus()
                                Return False
                            End If
                        End If
                    End If
                Next ChildControl
            End If
        Next Control
        Return True
    End Function

    Public Function validaDatePickers(ByVal form As Form) As Boolean
        For Each Control As Control In form.Controls
            If TypeOf Control Is DateTimePicker Then
                For Each ChildControl As DateTimePicker In Control.Controls
                    If ChildControl.Enabled = True Then
                        If ((Date.Parse(ChildControl.Text) < Date.Parse("01/01/1900")) Or (Date.Parse(ChildControl.Text) > Date.Parse(Date.Now))) Then
                            SetTheRegion(ChildControl, 2, 2, 4)
                            MessageBox.Show("La fecha ingresada es incorrecta")
                            ChildControl.Focus()
                            Return False
                        End If
                    End If
                Next ChildControl
            End If
        Next Control
        Return True
    End Function


    'Public Function validaRdButton(ByVal form As Form) As Boolean
    '    For Each element As RadioButton In form.Controls
    '        If element.Enabled = True Then
    '            If element.Checked = False Then
    '                MessageBox.Show("Falta seleccionar un dato")
    '                element.Focus()
    '                Return False
    '            End If
    '        End If
    '    Next element
    '    Return True
    'End Function


    Public Sub DrawTextBoxRectangles(ByVal form As Form, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim p As New Pen(Color.Red, 2)
        For Each element As TextBox In form.Controls
            e.Graphics.DrawRectangle(p, New Rectangle(element.Location + New Size(1, 1), element.Size - New Size(2, 2)))
        Next element
        p.Dispose()
    End Sub

    Public Sub DrawCmbBoxRectangles(ByVal form As Form, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim p As New Pen(Color.Red, 2)
        For Each element As ComboBox In form.Controls
            e.Graphics.DrawRectangle(p, New Rectangle(element.Location + New Size(1, 1), element.Size - New Size(2, 2)))
        Next element
        p.Dispose()
    End Sub

    Public Sub DrawDatePickersRectangles(ByVal form As Form, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim p As New Pen(Color.Red, 2)
        For Each element As DateTimePicker In form.Controls
            e.Graphics.DrawRectangle(p, New Rectangle(element.Location + New Size(1, 1), element.Size - New Size(2, 2)))
        Next element
        p.Dispose()
    End Sub


    Public Sub RellenoTextBoxRectangles(ByVal form As Form)
        For Each element As TextBox In form.Controls
            SetTheRegion(element, 0, 0, -4)
        Next element
    End Sub

    Public Sub RellenoCmbBoxRectangles(ByVal form As Form)
        For Each element As ComboBox In form.Controls
            SetTheRegion(element, 0, 0, -4)
        Next element
    End Sub

    Public Sub RellenoDatePickerRectangles(ByVal form As Form)
        For Each element As DateTimePicker In form.Controls
            SetTheRegion(element, 0, 0, -4)
        Next element
    End Sub

    '

    Private Sub SetTheRegion(ByVal Objeto As Object, ByVal X As Double, ByVal Y As Double, ByVal Borde As Double)
        Objeto.Region = New Region(New Rectangle(X, Y, Objeto.Width - Borde, Objeto.Height - Borde))
    End Sub





End Class


